package com.xdjd.mobile.third;

import com.alibaba.fastjson.JSON;
import com.xdjd.mobile.third.yuntong.request.SmsSendRequest;
import com.xdjd.mobile.third.yuntong.response.SmsSendResponse;
import com.xdjd.mobile.third.yuntong.util.ChuangLanSmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2019/1/9 0009.
 */
@Component
public class SendSmsService {
    private static final Logger logger = LoggerFactory.getLogger(SendSmsService.class);
    private static final String charset = "utf-8";
    // 请登录zz.253.com 获取创蓝API账号(非登录账号,示例:N1234567)
    @Value("${yuntong.account}")
    private String account;
    // 请登录zz.253.com 获取创蓝API密码(非登录密码)
    @Value("${yuntong.password}")
    private String password;
    //短信发送的URL 请登录zz.253.com 获取完整的URL接口信息
    @Value("${yuntong.request.url}")
    private String smsSingleRequestServerUrl;

    public boolean sendSms(String phone, String content) {
        //状态报告
        String report = "true";
        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, password, content, phone, report);
        String requestJson = JSON.toJSONString(smsSingleRequest);
        logger.info("before request string is: " + requestJson);
        String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        logger.info("response after request result is :" + response);
        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
        logger.info("response  toString is :" + smsSingleResponse);
        if(smsSingleResponse.getCode().equals("0")){
            return true;
        }
        return false;
    }
}
